﻿var binaryTree = new BinaryTree();
var canvas = document.getElementById("canvasBinaryTree").getContext("2d");
var dy = 50;

function Add() {
    var inputValue = document.getElementById("inputValue");
    var intValue = parseInt(inputValue.value);
    binaryTree.Add(intValue);
    UpdateCanvas();
}

function Reverse() {
    binaryTree.Reverse();
    UpdateCanvas();
}

function UpdateCanvas() {
    canvas.clearRect(0, 0, canvas.canvas.width, canvas.canvas.height);
    Draw(binaryTree.root, canvas.canvas.width / 2, 0, canvas.canvas.width / 2, canvas.canvas.width / 2);
}

function Draw(node, x, y, dx, dy) {
    if (node != null) {
        DrawLine(x, y, dx, y + 50);
        DrawItem(node.value, dx, y + 50);
        Draw(node.left, dx, y + 50, dx - dy / 2, dy / 2);
        Draw(node.right, dx, y + 50, dx + dy / 2, dy / 2);

    }
}

function DrawItem(value, x1, x2) {
    canvas.beginPath();
    canvas.arc(x1, x2, 25, 0, 2 * Math.PI, false);
    canvas.fillStyle = "#000000";
    canvas.fill();
    canvas.strokeStyle = "#000";
    canvas.stroke();
    canvas.font = "13pt Arial";
    canvas.fillStyle = "#FF0000";
    canvas.fillText(value, x1, x2);
}

function DrawLine(x1, y1, x2, y2) {
    canvas.beginPath();
    canvas.moveTo(x1, y1);
    canvas.lineTo(x2, y2);
    canvas.strokeStyle = "#000";
    canvas.stroke();
}